<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/
 
// no direct access
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
jimport( 'joomla.filesystem.path' );

jimport( 'joomla.database.database' );
include JPATH_COMPONENT.DS.'functions.php';
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class wotclanViewshow extends JView
{
    function display($tpl = null)
    {
    	if (JFactory::getUser()->authorise('core.add', 'com_wotclan')) 
      {
      	$db =& JFactory::getDbo();
      
      	if ($_GET["type"]=='joueur'){
    	  	$greeting = "Le joueur " .  $_GET["joueur"];
        	$this->assignRef( 'greeting', $greeting );
        	parent::display($tpl);
    			$query = $db->getQuery(true);
    			$query="SELECT * FROM `#__wotclan_account` WHERE `name`='". $_GET["joueur"]."'";
        	$db->setQuery($query);
        	$db->query();
        	$num_rows = $db->getNumRows();
        	$rows = $db->loadObjectList();
        	if ($num_rows ==1){
        		$tank = explode("|",$rows[0]->tanks);
        	//echo $row->tanks."<br>";
    				$x=0;
    				echo "<table>";
      			while ($tank[$x] != ""){
      				echo "<tr>";
      				
    		  		$query2 = $db->getQuery(true);
    		  		$query2="SELECT * FROM `#__wotclan_tanks` WHERE `uniqid`='".$tank[$x]."'";
        //  	echo "<br>".$query2."<br>";
    		  		$db->setQuery($query2);
      	  		$db->query();
      	  		$num_rows2 = $db->getNumRows();
      	  		$rows2 = $db->loadObjectList();
      	  		if ($rows2[0]->lvl == 10){
      	  			echo "<td>X </td>";
      	  	  } else if ($rows2[0]->lvl == 9){
      	  	  	echo "<td>IX </td>";
      	  	  } else if ($rows2[0]->lvl == 8){
      	  	  	echo "<td>VIII </td>";
      	  	  } else if ($rows2[0]->lvl == 7){
      	  	  	echo "<td>VII </td>";
      	  	  } else if ($rows2[0]->lvl == 6){
      	  	  	echo "<td>VI </td>";
      	  	  } else if ($rows2[0]->lvl == 5){
      	  	  	echo "<td>V </td>";
      	  	  } else if ($rows2[0]->lvl == 4){
      	  	  	echo "<td>IV </td>";
      	  	  } else if ($rows2[0]->lvl == 3){
      	  	  	echo "<td>III </td>";
      	  	  } else if ($rows2[0]->lvl == 2){
      	  	  	echo "<td>II </td>";
      	  	  } else if ($rows2[0]->lvl == 1){
      	  	  	echo "<td>I </td>";
      	  	  } else {
      	  	  	echo "<td> </td>";
      	  	  }
      	  		if ($num_rows2 == 1){
      	  			echo "<td>";
        				showimgtank($rows2[0]->name,$rows2[0]->common_name,$rows2[0]->tanktype,$tank[$x]);
        				echo "</td>";
        				
        		
    					} elseif ($num_rows2 == 0) {
          			echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : aucun tank</tr>";
          			if (preg_match("/".$tank[$x]."|/",$row->tanks)){
      					$newstring=preg_replace("/".$tank[$x]."|/","",$row->tanks);
      				
      					} else if (preg_match("/|".$tank[$x]."/",$row->tanks)){
									$newstring=preg_replace("/|".$tank[$x]."/","",$row->tanks);
			  				} else {
			  					$newstring=preg_replace("/".$tank[$x]."/","",$row->tanks);
			  				}
			  				$newstring=preg_replace("/\|$/","",$newstring);
			  				$query3 = $db->getQuery(true);
			  				$query3="UPDATE `#__wotclan_account` SET `tanks`='".$newstring."' WHERE `uniqid`='".$row->uniqid."';";
			  		
   	    				//$db->setQuery($query3);
      					//$db->query();
      					echo "<h1>newstring= ".$newstring." query : $query3</h1>";
          		//deletetank($tank[$x],$row->uniqid)
          		} else {
           			echo "<tr>Prob avec le tank ".$x."(".$tank[$x].") : trop de tank</tr>";
          		}
          		if ($_GET["debug"]){
          			echo " (".$tank[$x].") ";
          		}
          		//echo "<td><a href='?view=delete&tankid=".$tank[$x]."&account=".$row->uniqid."'>  Effacer</a></td>";
          		
        
        		echo "<tr>";
		    		$x++;
		    		}
		    	echo "</table>";
    		
  		  	}
  		  	
        	
      	}
      	if ($_GET["type"]=='tank'&& $_GET["id"]!=""){
  		  	
  		  	
  		$query = $db->getQuery(true);
    		$query="SELECT * FROM `#__wotclan_tanks` WHERE `uniqid`='". $_GET["id"]."'";
        	$db->setQuery($query);
        	//echo "<h1>".$query."</h1>";
        	$db->query();
        	$num_rows = $db->getNumRows();
        	$rows = $db->loadObjectList();
        	if ($num_rows == 1){
        		if ($rows[0]->common_name == ""){
        			$greeting = $rows[0]->name;
        		} else {
        			$greeting = $rows[0]->common_name;
        		}
        	$this->assignRef( 'greeting', $greeting );
        	parent::display($tpl);
        	}
        	echo "Tier ";
        	showlvl($rows[0]->lvl);
        	echo " ";
        	showimgtank($rows[0]->name,$rows[0]->common_name,$rows[0]->tanktype,"");
            echo '<br><a href="http://worldoftanks.com/encyclopedia/vehicles/',str_replace(':','/',$rows[0]->name),'" target="blank">Voir sur tankopedia</a>';


  		  	$query2 = $db->getQuery(true);

    		$query2="SELECT * FROM `#__wotclan_account` WHERE `clan`='BD' AND `tanks` LIKE '%".$rows[0]->uniqid."%'";
        	$db->setQuery($query2);
        	$db->query();
        	$num_rows2 = $db->getNumRows();
        	$rows2 = $db->loadObjectList();
            echo "<br>Il y a ",$num_rows2," joueurs dans le clan qui ont ce tank";
            foreach ($rows2 as $row2){
                echo '<br><a href="index.php/component/wotclan/component/wotclan/?view=show&type=joueur&joueur=',$row2->name,'">',$row2->name,'</a> [<a href="index.php/component/wotclan/component/wotclan/?view=search&clan=BD">BD</a>]';
      
            }







  		  }
      	if ($_GET["type"]=='replay'){
  		  	
  		  	

  		  	$query = $db->getQuery(true);
    		$query="SELECT * FROM `#__wotclan_replays` WHERE `uniqid`='". $_GET["id"]."'";
        	$db->setQuery($query);
        	//echo "<h1>".$query."</h1>";
        	$db->query();
        	$num_rows = $db->getNumRows();
        	$rows = $db->loadObjectList();
        	if ($num_rows == 1){
        		$greeting = $rows[0]->title;	
        		$this->assignRef( 'greeting', $greeting );
        		parent::display($tpl);
        		$query2 = $db->getQuery(true);
        		$query2="SELECT * FROM `#__wotclan_battle` WHERE `uniqid`='". $rows[0]->battle."'";
        		$db->setQuery($query2);
        		$db->query();
        		$num_rows2 = $db->getNumRows();
				$rows2 = $db->loadObjectList();
				if ($num_rows2 == 1){
					$team1=explode("|",$rows2[0]->team1);
					$team2=explode("|",$rows2[0]->team2);
					$tanks1=explode("|",$rows2[0]->tanks1);
					$tanks2=explode("|",$rows2[0]->tanks2);
					echo "<div align='center'>";
        
 					echo "<table><tr><td width='250' align='center' style='border:1px solid white'>Team 1</td><td width='250' align='center' style='border:1px solid white'>Vehicule</td><td width='250' align='center' style='border:1px solid white'>Vs</td><td width='250' align='center' style='border:1px solid white'>Team 2</td><td width='250' align='center' style='border:1px solid white'>Vehicule</td></tr>";
 					$x=0;
 					while (isset($team1[$x]) or isset($team2[$x])){
    					echo "<tr><td width='250' align='center' style='border:1px solid white'><a href='?view=show&type=joueur&joueur="
    					.$team1[$x]."'>".$team1[$x]."</a>";
    					//if ($team1['clan'][$x]!== ''){
        				//	echo "<a href='?view=search&clan=".$team1['clan'][$x]."'>[".$team1['clan'][$x]."]</a>";
    					//}
    					echo "</td><td width='250' align='center' style='border:1px solid white'>";
    					showimgtank($tanks1[$x],"","","");
    					echo "</td><td width='250' align='center' style='border:1px solid white'></td>".
    					"<td width='250' align='center' style='border:1px solid white'><a href='?view=show&type=joueur&joueur="
    					.$team2[$x]."'>".$team2[$x]."</a>";
    					//if ($team2['clan'][$x]!== ''){
        					//echo "<a href='?view=search&clan=".$team2['clan'][$x]."'>[".$team2['clan'][$x]."]</a>";
 						//}
    					echo "</td><td width='250' align='center' style='border:1px solid white'>";
    					showimgtank($tanks2[$x],"","","");
    					echo"</td></tr>";	
    					$x++;
 					}
					echo "</table>";
 					echo "</div>";
				}
        	}
        	
	//}  else {
	//	echo "<h1>Request missing infortmation !</h1>"		;
	}
      }
    }
}
